package com.hmall.api.config;

import com.hmall.api.feign.ItemRemote;
import com.hmall.api.feign.fallback.ItemRemoteFallback;
import com.hmall.api.feign.fallback.SearchRemoteFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 龙玉虎 yuhu.long@hand-china.com
 * @CreateTime: 2024-12-06
 * @Description: feign客户端日志级别
 */

@Configuration
public class DefaultFeignConfig {

    @Bean
    public Logger.Level feignLogLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 获取登录用户
                Long userId = UserContext.getUser();
                if(userId == null) {
                    // 如果为空则直接跳过
                    return;
                }
                // 如果不为空则放入请求头中，传递给下游微服务
                template.header("user-info", userId.toString());
            }
        };
    }


    @Bean
    public ItemRemoteFallback itemRemoteFallback() {
        return new ItemRemoteFallback();
    }

    @Bean
    public SearchRemoteFallback searchRemoteFallback() {
        return new SearchRemoteFallback();
    }
}
